from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import sys
from gui_qt5 import Ui_MainWindow
import requests
from bs4 import BeautifulSoup as bs
import threading
import keyboard
QCoreApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
class mainwindow(Ui_MainWindow,QMainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

        self.pushButton.clicked.connect(self.youdao)
        self.word.returnPressed.connect(self.youdao)
        self.setWindowTitle('英汉词典')
        self.keyopen = threading.Thread(target=self.keyopen_target)
        self.keyopen.start()
    def keyopen_target(self):
        while True:
            keyboard.add_hotkey('ctrl+alt+q',lambda: self.setHidden(False))
            keyboard.wait()
    def youdao(self):
        req = requests.get('https://dict.youdao.com/result?word=%s&lang=en'% self.word.text())
        doc = bs(req.text,'lxml')
        html_doc = doc.find_all(name='span',attrs={"class": "trans"})
        html_string = ""
        for element in html_doc:
            html_string += str(element)
        self.word_youdao.setHtml(html_string)
    def closeEvent(self,event):
        self.setHidden(True)
        event.ignore()
app = QApplication(sys.argv)
main = mainwindow()
main.show()
sys.exit(app.exec())